"""
@Project ：python
@File    ：file_define.py
@IDE     ：PyCharm
@Author  ：DongCha
@Date    ：2024/7/14上午7:32
"""
import json

from data_define import Record


# 文件读取类
class FileReader:
    # 读取文件数据，读取到每一条数据转换为Record对象，将他们封装到list中返回
    def read_data(self, ) -> list[Record]:
        pass


# 文本文件读取
class TextFileReader(FileReader):
    def __init__(self, path):
        self.path = path  # 记录文件路径

    # 复写父类方法
    def read_data(self) -> list[Record]:
        with open(self.path, 'r', encoding='utf-8') as f:
            record_list : list[Record] = []
            for line in f.readlines():
                line = line.strip()  # 消除每一行中的换行符
                data_list = line.split(",")
                record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
                record_list.append(record)
        return record_list


# JSON文件读取
class JsonFileReader(FileReader):
    def __init__(self, path):
        self.path = path

    # 复写父类方法
    def read_data(self) -> list[Record]:
        with open(self.path, 'r', encoding='utf-8') as f:
            record_list : list[Record] = []
            for line in f.readlines():
                data_dict = json.loads(line)
                record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"])
                record_list.append(record)
        return record_list


if __name__ == '__main__':
    textfilereader = TextFileReader("2011年1月销售数据.txt")
    jsonfilereader = JsonFileReader("2011年2月销售数据JSON.txt")
    list1 = textfilereader.read_data()
    list2 = jsonfilereader.read_data()
    for i in list1:
        print(i.__str__())

    for i in list2:
        print(i.__str__())

